package com.krahets88.trackback;

import java.util.LinkedList;
import java.util.Queue;

public class Question104 {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public int maxDepth(TreeNode root) {
            return preorder(root,0);
        }

        public int preorder(TreeNode root, int depth) {
            if (root == null) {
                return depth;
            }
            return Math.max(preorder(root.left, depth + 1), preorder(root.right, depth + 1));
        }
    }
    class SolutionTwo {
        public int maxDepth(TreeNode root) {
            if(root ==null){
                return 0;
            }
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        }


    }

    class SolutionThree{
        /**
         * 层序遍历解法
         * @param root
         * @return
         */
        public int maxDepth(TreeNode root) {
            LinkedList<TreeNode> queue = new LinkedList<>();
            if(root!=null){
                queue.add(root);
            }
            int h = 0;
            while (queue.size()!=0){
                int size = queue.size();
                for (int i =0 ; i<size;i++){
                    TreeNode currentNode = queue.remove();
                    if(currentNode.left!=null){
                        queue.add(currentNode.left);
                    }
                    if(currentNode.right!=null){
                        queue.add(currentNode.right);
                    }
                }
                h++;
            }
            return h;
        }
    }
}
